package com.example.osixaffairssystem.controller;

import com.example.osixaffairssystem.domain.PageBean;
import com.example.osixaffairssystem.domain.Result;
import com.example.osixaffairssystem.domain.model.Emp;
import com.example.osixaffairssystem.service.EmpService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emp")
@RequiredArgsConstructor
public class EmpController {

    final EmpService empService;

    @GetMapping("/list")
    public Result list() {
        log.info("获取员工列表");
        List<Emp> list = empService.list();
        return Result.success(list);
    }

    @GetMapping
    public Result listToPage(@RequestParam(name = "page") Integer page,
                             @RequestParam(name = "pageSize") Integer pageSize) {
        log.info("分页查询员工{ page: {}, pageSize: {} }", page, pageSize);
        PageBean<Emp> list = empService.listToPage(page, pageSize);
        return Result.success(list);
    }

    @PostMapping
    public Result insert(@RequestBody Emp emp) {
        log.info("新增员工: {}", emp);
        Integer id = empService.insert(emp);
        return Result.success(id);
    }

    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工: {}", emp);
        empService.update(emp);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("删除员工: {}", ids);
        empService.delete(ids);
        return Result.success();
    }

}
